const service = require('../../service');
const BaseCtrler = require('../baseController');
const configurations = require('../../config');

class AssetFileCtrler extends BaseCtrler {
  businessLogic = async (params) => {
    const { path, clientId, } = params;

    const contract_info = await service.fileService.parseContract(path, clientId);

    return {
      status: 200,
      info: { contract_info },
    };
  };

  verifyReq = (req) => {
    const { files, body, } = req;
    const { clientId, createtime } = body;

    if (!clientId) {
      return {
        status: 400,
        errMsg: `lack websocket client_id, please use clientId parameter apply`,
      }
    }

    if (Array.isArray(files)) {
      const { originalname, } = files[0];
      const path = `${configurations.common.uploadPath}/${originalname}`;

      return {
        params: { path, clientId},
        errMsg: '',
      };
    } else {
      return {
        status: 400,
        errMsg: 'did not upload any file',
      };
    }
  };
}

let fileCtrlInstance;
const getCtrlerInstance = () => {
  if (!fileCtrlInstance) {
    fileCtrlInstance = new AssetFileCtrler();
  }

  return fileCtrlInstance;
};

const assetFile = async (req, res) => {
  getCtrlerInstance();

  const result = await fileCtrlInstance.ctrl(req);
  return result;
};
module.exports = assetFile;
